home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-06-28 | 6.1 KB | 193 lines | [TEXT/EDIT] |
- Mike Babulic (Compuserve ID 72307,314)
- 3827 Charleswood Dr. N.W., Calgary, Alberta
- CANADA,
- T2L 2C7
- .
- One of the weaknesses of TML Pascal is a lack of procedure parameters.
- My solution was write the UNIT "ProcParms". It exports two procedures:
- ProcParm and JumpTo(p:ProcPtr).
- .
- To use procedure parameters in your program you write a "dummy" procedure
- for each procedure you wish to pass as a parameter. The dummy procedure has
- exactly the same parameters as the "passed procedure" except that it also has
- a pointer to the "procedure parameter" as its very last parameter (see the
- example). It has no local declarations, and the only executable code is a
- call to "ProcParm".
- .
- ProcParm does 4 things:
- 1) it restores the stack to look exactly like it did on entry to the
- dummy procedure.
- 2) it moves the last parameter (address of the "passed procedure") with
- the dummy's return address from the stack to A0.
- 3) it moves the return address "down" the stack to where the last
- parameter used to be. THE STACK IS NOW EXACTLY AS IT WOULD HAVE BEEN IF
- IF THE "PASSED PROCEDURE" HAD BEEN CALLED instead of the dummy!
- 4) it jumps to A0 (the start of the passed procedure)
- .
- JumpTo(p:ProcPtr) works in a simaler way to ProcParm, but its purpose is
- quite different. It redirects a call to the dummy procedure to the
- procedure pointed to by "p". The ProcPtr is supplied as an parameter of
- JumpTo, rather than as the last parameter of the dummy procedure.
- .
- One word of warning: ALL of the passed & dummy procedures _MUST_ be GLOBAL
- procedures.
- .
- ****************************** Example.pas *********************************
-
- { Shows how to use the ProcParms unit
-
- Author: Mike Babulic (Compuserve ID 72307,314)
- 3827 Charleswood Dr. N.W.,
- Calgary, Alberta
- CANADA, T2L 2C7
- }
-
- PROGRAM Example(input,output);
-
- USES MacIntf, ProcParms;
-
- (* {Lisa Pascal}
- {$U ProcParms}
-
- USES MemTypes, ProcParms;
- *)
-
- {----------------- Procedures to be passed as parameters -----------------}
- {n.b.Procedures MUST be GLOBAL}
-
- PROCEDURE theFirst(a,b:integer);
- begin
- WriteLn(' FIRST ',a:3,b:3);
- end;
-
- PROCEDURE theSecond(a,b:integer);
- begin
- WriteLn(a:3,' SECOND',b:3);
- end;
-
- PROCEDURE theThird(a,b:integer);
- begin
- WriteLn(a:3,b:3,' THIRD');
- end;
-
- {-------------- Shell to call procedures parameters with --------------}
-
- PROCEDURE dummy(a,b:integer;p:ProcPtr); {n.b.MUST be GLOBAL procedure}
- begin
- ProcParm;
- end;
-
- {------------------ Routine with procedure parameter ------------------}
-
- PROCEDURE OneTwo(p:ProcPtr);
- begin
- dummy(9,10,p);
- end;
- {------------- Using "JumpTo" to redirect a procedure call ------------}
-
- PROCEDURE Three(a,b:integer);
- begin
- JumpTo(@theThird);
- end;
-
- {----------------------------------------------------------------------}
-
- VAR ProcArray : ARRAY [1..3] OF ProcPtr;
- i : integer;
- c:char;
- BEGIN
-
- {Using Procedure Parameters}
- OneTwo(@theFirst); {will Execute theFirst}
- OneTwo(@theSecond); {will Execute theSecond}
-
- {Using JumpTo to redirect a procedure call}
- Three(11,12); {will Execute theThird}
- WriteLn;
-
- {Another use of the ProcParm "dummy" routine}
- ProcArray[1] := @theFirst;
- ProcArray[2] := @theSecond;
- ProcArray[3] := @theThird;
- for i := 3 downto 1 do
- dummy(i,i+1,ProcArray[i]);
-
- WriteLn; WriteLn('Press RETURN to Quit');
- Read(c);
- END.
-
- ***************************** ProcParms.pas ********************************
-
- UNIT ProcParms;
- {
- ;Version 2.0 ProcParms 86/10/10
-
- ;Author: Mike Babulic (Compuserve ID 72307,314)
- ; 3827 Charleswood Dr. N.W., Calgary, Alberta
- ; CANADA, T2L 2C7
- ; "Jump to ProcPtr" routine for Pascal
- ; % allows procedures to be passed as parameters
- ; % allows a procedure to be executed indirectly, given its address
- ; % Example.pas shows how it is used
- ;
- ; One of the weaknesses of TML (and other Mac Pascals) is that it doesn't
- ; implement procedure and function parameters. This UNIT offers a method of
- ; overcoming that restriction.
- ;
- ; The ProcParm procedure allows you to write procedure/function parameters.
- ;
- ; The JumpTo procedure redirects a procedure call.
- ;
- ; ***** IMPORTANT ***** ALL of the passed & dummy procedures _MUST_
- ; be GLOBAL procedures.
- ;
- ; see pp.C-4 thru C-6 of the TML manual to puzzle out
- ; how this works (or should work anyway)
- ;
-
- }
- { Refer to EXAMPLE.PAS to see how this unit is meant to be used}
-
- INTERFACE
-
- {$U ProcParm.REL}
-
- USES MacIntf;
-
- PROCEDURE ProcParm; EXTERNAL;
-
- PROCEDURE JumpTo(p:ProcPtr); EXTERNAL;
-
- IMPLEMENTATION
-
- END.
-
-
- ****************************** ProcParm.asm ********************************
-
- ;Version 2.0 ProcParm 86/10/07
- ;Author: Mike Babulic (Compuserve ID 72307,314)
- ; 3827 Charleswood Dr. N.W., Calgary, Alberta
- ; CANADA, T2L 2C7
- ; "Jump to ProcPtr" routine for Pascal
- ; % allows procedures to be passed as parameters
- ; % allows a procedure to be executed indirectly, given its address
- ; % Example.pas shows how it is used
- ;
- ; see pp.C-4 thru C-6 of the TML manual to puzzle out
- ; how this works (or should work anyway)
- ;
- XDEF ProcParm
- ProcParm
- UNLK A6 ;restore frame pointer - A6 & stack - SP
- MOVE.L 4(SP),A0 ;get procedure pointer
- MOVE.L (SP),4(SP) ;write return address where the ProcPtr used to be
- ADD.L #4,SP ;pop stack (postincrement cause illegal instruction)
- JMP (A0) ;jump to procedure
- ;
- XDEF JumpTo
- JumpTo
- MOVE.L 4(SP),A0 ;get procedure pointer
- UNLK A6 ;restore frame pointer - A6 & stack - SP
- JMP (A0) ;jump to procedure
-